package com.wego.controller;

import com.wego.bean.StateBean;
import com.wego.bean.TableBean;
import com.wego.common.bean.PageBean;
import com.wego.entity.domain.Cart;
import com.wego.entity.domain.CartItem;
import com.wego.entity.domain.Goods;
import com.wego.entity.domain.User;
import com.wego.entity.query.CartItemQuery;
import com.wego.entity.vo.CartItemVO;
import com.wego.service.CartItemService;
import com.wego.service.CartService;
import com.wego.service.GoodsService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


/**
 * (tb_cart_item)表控制层
 *
 * @author xxxxx
 */
@Controller
@RequestMapping("user/cartItem")
public class CartItemController {

    @Autowired
    private CartItemService cartItemService;

    @Autowired
    private CartService cartService;

    @Autowired
    private GoodsService goodsService;

    @GetMapping("/list")
    public @ResponseBody TableBean list(Long userId,CartItemQuery cartItemQuery) {
        Cart cart = cartService.selectByPrimaryKey(userId);
        if(cart != null)
            cartItemQuery.setCartId(cart.getId());
        //构造动态查询条件
        final PageBean<CartItem> cartItemPageBean = cartItemService.selectPage(cartItemQuery);
        PageBean<CartItemVO> pageBean = cartItemPageBean2CartItemVOPageBean(cartItemPageBean);
        List<CartItemVO> cartItemVOS = pageBean.getResult();
        Long count = pageBean.getTotal();
        TableBean tableBean = new TableBean<CartItemVO>();
        tableBean.setCode(0);
        tableBean.setMsg("");
        tableBean.setData(cartItemVOS);
        tableBean.setCount(count);
        return tableBean;
    }

    private PageBean<CartItemVO> cartItemPageBean2CartItemVOPageBean(PageBean<CartItem> pageBean) {
        PageBean<CartItemVO> cartItemVOPageBean = new PageBean<>();
        cartItemVOPageBean.setPageNum(pageBean.getPageNum());
        cartItemVOPageBean.setPageSize(pageBean.getPageSize());
        cartItemVOPageBean.setPages(pageBean.getPages());
        cartItemVOPageBean.setTotal(pageBean.getTotal());

        List<CartItemVO> list = new ArrayList<>();
        for (CartItem cartItem : pageBean.getResult()) {
            final CartItemVO cartItemVO = cartItem2CartItemVO(cartItem);
            list.add(cartItemVO);
        }
        cartItemVOPageBean.setResult(list);

        return cartItemVOPageBean;
    }

    private CartItemVO cartItem2CartItemVO(CartItem cartItem) {
        if (cartItem == null) {
            return null;
        }
        CartItemVO cartItemVO = new CartItemVO();
        cartItemVO.setId(cartItem.getId());
        final Goods goods = goodsService.selectByPrimaryKey(cartItem.getGoodsId());
        cartItemVO.setGoodsId(cartItem.getGoodsId());
        cartItemVO.setName(goods.getName());
        cartItemVO.setPic(goods.getPic());
        cartItemVO.setPrice2(goods.getPrice2());
        cartItemVO.setStorage(cartItem.getAmount());
        cartItemVO.setTotal(new BigDecimal(cartItem.getAmount()).multiply(goods.getPrice2()).setScale(2, RoundingMode.HALF_UP));
        return cartItemVO;
    }

    @GetMapping("/updateNum")
    public @ResponseBody StateBean updateNum(CartItem cartItem) {
        cartItemService.updateByPrimaryKeySelective(cartItem);
        return new StateBean(200,"成功");
    }

    @GetMapping("/addItem")
    public @ResponseBody StateBean addItem(CartItem cartItem, HttpSession session) {
        User user = (User)session.getAttribute("user");
        Cart cart = null;
        if(user != null){
            Long id = user.getId();
            cart = cartService.selectByPrimaryKey(id);
        }
        else
            return new StateBean(0,"失败");
        if(cart != null)
            cartItem.setCartId(cart.getId());
        int num = cartItem.getAmount();
        cartItem.setAmount(null);
        if(cartItemService.selectCount(cartItem) > 0){
            cartItem = cartItemService.selectOne(cartItem);
            cartItem.setAmount(cartItem.getAmount() + num);
            cartItem.setUpdateTime(LocalDateTime.now());
            cartItemService.updateByPrimaryKeySelective(cartItem);
        }
        else{
            cartItem.setAmount(num);
            cartItem.setCreateTime(LocalDateTime.now());
            cartItem.setUpdateTime(LocalDateTime.now());
            cartItemService.insertSelective(cartItem);
        }

        return new StateBean(200,"成功");
    }

    @GetMapping("/deleteOne")
    public @ResponseBody StateBean deleteOne(Long id) {
        if(cartItemService.deleteByPrimaryKey(id))
            return new StateBean(200,"成功");
        else
            return new StateBean(0,"失败");
    }

    @GetMapping("/userCart")
    public String goToHome(CartItem cartItem,Model model, HttpSession session) {
        User user = (User)session.getAttribute("user");
        Cart cart = null;
        if(user != null){
            Long id = user.getId();
            cart = cartService.selectByPrimaryKey(id);
        }
        else
            return "user/unauthorized";
        if(cart != null)
            cartItem.setCartId(cart.getId());
        int itemNum = cartItemService.selectCount(cartItem);
                model.addAttribute("itemNum",String.valueOf(itemNum));
        return "user/goodsCar";
    }
}
